package logic

import (
	"context"
	"fmt"
	"gorm.io/gorm"
	"home-manage/homerpc/homerpc"
	"home-manage/homerpc/internal/svc"
	"home-manage/model"

	"github.com/zeromicro/go-zero/core/logx"
)

type AppointmentUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAppointmentUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AppointmentUpdateLogic {
	return &AppointmentUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AppointmentUpdateLogic) AppointmentUpdate(in *homerpc.AppointmentUpdateRequest) (*homerpc.AppointmentUpdateResponse, error) {
	// todo: add your logic here and delete this line

	begin := model.DB.Begin()
	appointment := model.Appointment{
		Model: gorm.Model{
			ID: uint(in.AppointmentId),
		},
		Status: "已取消",
	}
	err := begin.Updates(&appointment).Error
	if err != nil {
		begin.Rollback()
		return nil, fmt.Errorf("取消失败")
	}
	begin.Commit()
	return &homerpc.AppointmentUpdateResponse{}, nil
}
